/**
 * 车间线体设备用量
 */
var energyDevId = {
    "summarySteams": {
        workshop : 10324,
        line : 10326
    },//蒸汽
    "aggregateWaters": {
        workshop : 10317,
        line : 10318
    },//用水
    "aggregateGass": {
        workshop : 10310,
        line : 10311
    },//燃气
    "aggregateElectricitys": {
        workshop : 10303,
        line : 10304
    },//用电
    "airCollectionss": {
        workshop : 10294,
        line : 10295
    },//空气
    "aggregateCoolings": {
        workshop : 10286,
        line : 10287
    }//冷量
};
dev.action = function(resp,param){

    var spaceName = param.workSpaceName;
    var name = param.name;
    // 获取能源架构ID
    var workSpace = iot.getEnergySpaceId(spaceName,name);
    if (workSpace === -1){
        resp.setJsData("找不到数据");
        return;
    }

    var workshopParam= {
        id:energyDevId[name].workshop,
        time: param.time,
        selectType: param.selectType,
        workSpace:workSpace,
        workSpaceName: spaceName
    }

    // 获取车间用量
    var workshopResult = iot.bridgeOtherJsResult(JSON.stringify(workshopParam));

    var lineParam= {
        id:energyDevId[name].line,
        time: param.time,
        selectType: param.selectType,
        workSpace:workSpace,
        workSpaceName: spaceName
    }

    // 获取线体用量
    var lineResult = iot.bridgeOtherJsResult(JSON.stringify(lineParam));

    var totalSum = 0;
    var datas = lineResult.jsData.datas;
    for (var i in datas) {
        var value = Number(datas[i].value);
        totalSum += value;
    }

    // 获取设备用量
    var db = iot.db();
    var equipmentResult = db.queryOne("select SUM(win.dvalue) totalSum\n" +
        "from energy_struc_prop esp\n" +
        "inner join energy_struc es1 on esp.energy_struc_id = es1.id\n" +
        "inner join iot_dev_data_win win on esp.dev_id = win.dev_id\n" +
        "inner join energy_struc es2 on es1.pid = es2.id\n" +
        "where\n" +
        "esp.type = 1 and esp.field_type = '用量'\n" +
        "and esp.energy_struc_id = es1.id\n" +
        "and es2.pid = "+ workSpace +"\n" +
        "and win.data_type = esp.name\n" +
        "and win.the_time like '"+ param.time +"%'");


    var result = {
        workshon : workshopResult.jsData.datas[0].value,
        line : totalSum + '',
        equipment : equipmentResult.totalSum + ''
    }

    resp.setJsData(result);
}